{% extends "saltstack/index.html" %}

{% block right_content %}
<div id="right_menu">
    <form class="form-inline" style="float:center">
        <div class="form-group">
            <label class="sr-only" for="hostname">Report</label>
            <input type="text" class="form-control" id="hostname" placeholder="主机名"  style="width:300" value="{{ hostname }}">
        </div>
        <button type="button" class="btn btn-primary" id="assetReport">上报资产</button>
    </form>
    <hr style="border:1 dashed #987cb9;margin-left:1px;color:#987cb9;size:10">
    <span style="display:inline" id="warningInfo"></span>
</div>

<script type="text/javascript">
    $("#assetReport").click(function(){
        $("#warningInfo").html("");
        var hostname = $("#hostname").val();
        //cpu
        var urls = "{% url 'saltstack:getcmd' %}";
        var salt_cmd = 'dmidecode -t memory|grep -i size |grep -v -E "Maximum|Enabled|Installed"'
        var postdata = {salt_node_name:hostname,comm_shell:'cmd',salt_cmd:salt_cmd}
        var jsonstr = $.ajax({
			    url : urls,
			    type : "POST",
			    data : postdata,
			    async : false,
			    cache : false
			});
		var jsonobj = eval('(' + jsonstr.responseText + ')');
		memory = jsonobj.data;

		//other data info
        var urls = "{% url 'equipment:equniquecheck' %}";
        var postdata = {hostname:hostname}
        var jsonstr = $.ajax({
			    url : urls,
			    type : "POST",
			    data : postdata,
			    async : false,
			    cache : false
			});
	    var jsonobj = eval('(' + jsonstr.responseText + ')');
	    if(jsonobj.status == 1){
	        if(jsonobj.data == false){
	            window.location.href="{% url 'saltstack:assetReport' %}?hostname=" + hostname + "&memory=" + memory;
	        }else{
	            $("#warningInfo").html("<font size='2' style='color:red'>设备已存在</font>");
	        }
	    }else{
	        $("#warningInfo").html("<font size='2' style='color:red'>" + jsonobj.message + "</font>");
	    }
    });
</script>
{% endblock %}
